{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `networkconfgen` examples\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from networkconfgen import NetworkConfGen\n",
    "\n",
    "confgen = NetworkConfGen()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic usage\n",
    "\n",
    "Basic usage with `str` objects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "networkconfgen.base.NetworkConfGenResult"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "template = \"\"\"\n",
    "!\n",
    "hostname {{ hostname }}\n",
    "!\n",
    "\"\"\"\n",
    "\n",
    "parameters = {\n",
    "    \"hostname\": \"demo\"\n",
    "}\n",
    "\n",
    "result = confgen.render_from_string(template_content=template, parameters=parameters)\n",
    "type(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Verify that the parsing process was successful"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.render_error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Display the results (raw and cleaned)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "!\n",
      "hostname demo\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "print(result.template_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## raw vs. cleaned result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!\n",
      "    somthing is defined\n",
      "!\n",
      "interface Ethernet 0/1\n",
      "     ip address dhcp\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "template = \"\"\"\\\n",
    "!\n",
    "{% if something %}\n",
    "    somthing is defined\n",
    "{% endif %}\n",
    "!\n",
    "interface Ethernet 0/1\n",
    "{% if something_else %}\n",
    "    {# use 5 blanks to maintain readablility in the clean output#}\n",
    "     ip address dhcp\n",
    "{% endif %}\n",
    "!\"\"\"\n",
    "\n",
    "parameters = {\n",
    "    \"something\": True,\n",
    "    \"something_else\": True\n",
    "}\n",
    "result = confgen.render_from_string(template_content=template, parameters=parameters)\n",
    "\n",
    "# view the raw output\n",
    "print(result.template_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!\n",
      "somthing is defined\n",
      "!\n",
      "interface Ethernet 0/1\n",
      " ip address dhcp\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "print(result.cleaned_template_result())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## content error checks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "template = \"\"\"\\\n",
    "!\n",
    "hostname {{ hostname|default(_ERROR_.invalid_value) }}\n",
    "!\"\"\"\n",
    "\n",
    "result = confgen.render_from_string(template_content=template, parameters={})\n",
    "result.render_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.content_error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'content_error': True,\n",
       " 'error_text': None,\n",
       " 'from_string': True,\n",
       " 'render_error': False,\n",
       " 'search_path': None,\n",
       " 'template_file_name': None,\n",
       " 'template_result': '!\\nhostname $$INVALID_VALUE$$\\n!'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
